package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.ProductBean;
import com.project.service.IProductService;
import com.project.mapper.IProductMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.List;

public class ProductServiceImpl extends BaseService implements IProductService {
    @Override
    public void add(ProductBean product) {
        SqlSession session = this.getSession();
        try {
            IProductMapper mapper = session.getMapper(IProductMapper.class);
            mapper.add(product);

            session.commit();
        }catch (Exception e){
            e.printStackTrace();
            session.rollback();
        }finally {
            session.close();
        }
    }

    @Override
    public void del(int id) {
        SqlSession session = this.getSession();
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        mapper.del(id);

        session.commit();
        session.close();
    }

    @Override
    public void update(int id, int price) {
        SqlSession session = this.getSession();
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        mapper.update(id,price);

        session.commit();
        session.close();
    }

    @Override
    public void addMore(List<ProductBean> productList) {
        SqlSession session = this.getSession();
        try {
            IProductMapper mapper = session.getMapper(IProductMapper.class);
            mapper.addMore(productList);
            session.commit();
        }catch (Exception e){
            e.printStackTrace();
            session.rollback();
        }finally {
            session.close();
        }
    }

    @Override
    public void delMore(@Param("idArr") int[] idArr) {
        SqlSession session = this.getSession();
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        mapper.delMore(idArr);
        session.commit();
        session.close();
    }

    @Override
    public List<ProductBean> findByDate(LocalDate startDate, LocalDate endDate) {
        SqlSession session = this.getSession();
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        List<ProductBean> list = mapper.findByDate(startDate,endDate);
        session.close();
        return list;
    }

    @Override
    public List<ProductBean> findAll() {
        SqlSession session = this.getSession();
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        List<ProductBean> list = mapper.findAll();
        session.close();
        return list;
    }

    @Override
    public CutPageBean<ProductBean> findByItem(int num, String name, String type, int startPrice, int endPrice) {
        CutPageBean<ProductBean> productCutPageBean = new CutPageBean<ProductBean>();
        SqlSession session = this.getSession();
        IProductMapper mapper = session.getMapper(IProductMapper.class);
        //得到当前页需要显示的数据
        List<ProductBean> list = mapper.findByItem((num-1)*PAGE_SIZE,name,type,startPrice,endPrice,PAGE_SIZE);
        //得到总记录数
        int count = mapper.getCount();
        //得到总页数
        int totalPage = count%PAGE_SIZE==0?count/PAGE_SIZE:count/PAGE_SIZE+1;
        productCutPageBean.setList(list);
        productCutPageBean.setTotalPage(totalPage) ;
        productCutPageBean.setCount(count);
        session.close();
        return productCutPageBean;
    }


}
